home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / diff / test.c < prev   
Encoding:
C/C++ Source or Header  |  2001-08-22  |  4.2 KB  |  193 lines

  1. /* diff/test.c
  2.  * 
  3.  * Copyright (C) 2000 David Morrison
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <gsl/gsl_math.h>
  25. #include <gsl/gsl_diff.h>
  26. #include <gsl/gsl_errno.h>
  27. #include <gsl/gsl_test.h>
  28.  
  29. double
  30. f1 (double x, void *params)
  31. {
  32.   return exp (x);
  33. }
  34.  
  35. double
  36. df1 (double x, void *params)
  37. {
  38.   return exp (x);
  39. }
  40.  
  41. double
  42. f2 (double x, void *params)
  43. {
  44.   if (x >= 0.0)
  45.     {
  46.       return x * sqrt (x);
  47.     }
  48.   else
  49.     {
  50.       return 0.0;
  51.     }
  52. }
  53.  
  54. double
  55. df2 (double x, void *params)
  56. {
  57.   if (x >= 0.0)
  58.     {
  59.       return 1.5 * sqrt (x);
  60.     }
  61.   else
  62.     {
  63.       return 0.0;
  64.     }
  65. }
  66.  
  67. double
  68. f3 (double x, void *params)
  69. {
  70.   if (x != 0.0)
  71.     {
  72.       return sin (1 / x);
  73.     }
  74.   else
  75.     {
  76.       return 0.0;
  77.     }
  78. }
  79.  
  80. double
  81. df3 (double x, void *params)
  82. {
  83.   if (x != 0.0)
  84.     {
  85.       return -cos (1 / x) / (x * x);
  86.     }
  87.   else
  88.     {
  89.       return 0.0;
  90.     }
  91. }
  92.  
  93. double
  94. f4 (double x, void *params)
  95. {
  96.   return exp (-x * x);
  97. }
  98.  
  99. double
  100. df4 (double x, void *params)
  101. {
  102.   return -2.0 * x * exp (-x * x);
  103. }
  104.  
  105. double
  106. f5 (double x, void *params)
  107. {
  108.   return x * x;
  109. }
  110.  
  111. double
  112. df5 (double x, void *params)
  113. {
  114.   return 2.0 * x;
  115. }
  116.  
  117. double
  118. f6 (double x, void *params)
  119. {
  120.   return 1.0 / x;
  121. }
  122.  
  123. double
  124. df6 (double x, void *params)
  125. {
  126.   return -1.0 / (x * x);
  127. }
  128.  
  129. typedef int (diff_fn) (const gsl_function * f, double x, double * res, double *abserr);
  130.  
  131. void
  132. test (diff_fn * diff, gsl_function * f, gsl_function * df, double x, 
  133.       const char * desc)
  134. {
  135.   double result, abserr;
  136.   double expected = GSL_FN_EVAL (df, x);
  137.   (*diff) (f, x, &result, &abserr);
  138.   gsl_test_abs (result, expected, abserr, desc);
  139.   gsl_test (fabs(result-expected) >  abserr, "%s, valid error estimate", desc);
  140. }
  141.  
  142. int
  143. main ()
  144. {
  145.   gsl_function F1, DF1, F2, DF2, F3, DF3, F4, DF4, F5, DF5, F6, DF6;
  146.  
  147.   F1.function = &f1;
  148.   DF1.function = &df1;
  149.  
  150.   F2.function = &f2;
  151.   DF2.function = &df2;
  152.  
  153.   F3.function = &f3;
  154.   DF3.function = &df3;
  155.  
  156.   F4.function = &f4;
  157.   DF4.function = &df4;
  158.  
  159.   F5.function = &f5;
  160.   DF5.function = &df5;
  161.  
  162.   F6.function = &f6;
  163.   DF6.function = &df6;
  164.   
  165.   test (&gsl_diff_central, &F1, &DF1, 1.0, "exp(x), x=1, central diff");
  166.   test (&gsl_diff_forward, &F1, &DF1, 1.0, "exp(x), x=1, forward diff");
  167.   test (&gsl_diff_backward, &F1, &DF1, 1.0, "exp(x), x=1, backward diff");
  168.  
  169.   test (&gsl_diff_central, &F2, &DF2, 0.1, "x^(3/2), x=0.1, central diff");
  170.   test (&gsl_diff_forward, &F2, &DF2, 0.1, "x^(3/2), x=0.1, forward diff");
  171.   test (&gsl_diff_backward, &F2, &DF2, 0.1, "x^(3/2), x=0.1, backward diff");
  172.  
  173.   test (&gsl_diff_central, &F3, &DF3, 0.45, "sin(1/x), x=0.45, central diff");
  174.   test (&gsl_diff_forward, &F3, &DF3, 0.45, "sin(1/x), x=0.45, forward diff");
  175.   test (&gsl_diff_backward, &F3, &DF3, 0.45, "sin(1/x), x=0.45, backward diff");
  176.  
  177.   test (&gsl_diff_central, &F4, &DF4, 0.5, "exp(-x^2), x=0.5, central diff");
  178.   test (&gsl_diff_forward, &F4, &DF4, 0.5, "exp(-x^2), x=0.5, forward diff");
  179.   test (&gsl_diff_backward, &F4, &DF4, 0.5, "exp(-x^2), x=0.5, backward diff");
  180.  
  181.   test (&gsl_diff_central, &F5, &DF5, 0.0, "x^2, x=0, central diff");
  182.   test (&gsl_diff_forward, &F5, &DF5, 0.0, "x^2, x=0, forward diff");
  183.   test (&gsl_diff_backward, &F5, &DF5, 0.0, "x^2, x=0, backward diff");
  184.  
  185.   test (&gsl_diff_central, &F6, &DF6, 10.0, "1/x, x=10, central diff");
  186.   test (&gsl_diff_forward, &F6, &DF6, 10.0, "1/x, x=10, forward diff");
  187.   test (&gsl_diff_backward, &F6, &DF6, 10.0, "1/x, x=10, backward diff");
  188.  
  189.   exit (gsl_test_summary ());
  190. }
  191.  
  192.  
  193.